package com.bestcem.xm.qdes.handler.export.type;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.qdes.enums.QuestionCustomTypeEnum;
import com.bestcem.xm.qdes.enums.QuestionTypeEnum;
import com.bestcem.xm.qdes.handler.export.BaseTypeHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Slf4j
@Component
public class MatrixMultipleHandler implements BaseTypeHandler {

    @Override
    public List<Object> getItems(JSONObject questionDict, JSONObject qgidDict, List<JSONObject> relatedRefs) {
        // 矩阵多选题
        //MatrixSingleHandler singleHandler = new MatrixSingleHandler();
        //List<Object> items = singleHandler.getItems(questionDict, qgidDict, relatedRefs);
        List<Object> items = getItems4MatrixSingle(questionDict, qgidDict, relatedRefs);
        String qcid = questionDict.getString("cid");
        String qtitle = questionDict.getString("title");
        String qtype = questionDict.getString("qtype");
        String customQtype = questionDict.getString("custom_qtype");

        int dispSelect = (int) questionDict.getOrDefault("dispSelect", 0);  // dispSelect:0 选项设置; 1 代码设置
        boolean codeSelect = (boolean) questionDict.getOrDefault("codeSelect", true); // true: 显示代码/false: 抹灰代码

        int jumpSelect = (int) questionDict.getOrDefault("jumpSelect", 0);  // jumpSelect:0 选项设置; 1 代码设

        String dispCode = (String) questionDict.getOrDefault("disp_code", "");  // 显示代码
        String jumpCode = (String) questionDict.getOrDefault("jump_code", "");  // 跳转代码
        String autoCode = (String) questionDict.getOrDefault("auto_code", "");  // 自动勾选代码

        boolean swap = (Boolean) questionDict.getOrDefault("swap", false); // 行列互换
        boolean required = questionDict.getBoolean("required");  // 必答

        List<Integer> optionsRange = castList(questionDict.getOrDefault("options_range", new ArrayList<>(Arrays.asList(0, 0))), Integer.class);
        String typeStr = "";
        try {
            typeStr = QuestionTypeEnum.valueOf("QUESTION_TYPE_" + qtype.toUpperCase()).getDesc();
        } catch (IllegalArgumentException exp) {
            log.error("No found QuestionTypeEnum");
        }
        try {
            QuestionCustomTypeEnum customQtypeDesc = QuestionCustomTypeEnum.valueOf("QUESTION_CUSTOM_TYPE_" + customQtype.toUpperCase());
            if (StringUtils.isNotEmpty(customQtypeDesc.getDesc())) {
                typeStr = customQtypeDesc.getDesc();
            }
        } catch (IllegalArgumentException exp) {
            log.error("No found QuestionCustomTypeEnum");
        }

        List<String> item0 = new ArrayList<>();
        item0.add(qcid + "[" + typeStr + "]");
        item0.add(qtitle);

        List<String> qAttr = new ArrayList<>();
        qAttr.add(required ? "必答" : "非必答");
        if (swap) {
            qAttr.add("行列互换");
        }
        if (optionsRange.get(0) != 0) {
            qAttr.add(String.format("最少选%s项", optionsRange.get(0)));
        }
        if (optionsRange.get(1) != 0) {
            qAttr.add(String.format("最多选%s项", optionsRange.get(1)));
        }

        if (dispSelect == 1 && codeSelect) {
            qAttr.add("显示代码");
        } else if (dispSelect == 1 && !codeSelect) {
            qAttr.add("抹灰代码");
        } else {
            qAttr.add("");
        }

        // 题目属性
        List<String> allAttr = new ArrayList<>();
        qAttr.forEach(at -> {
            allAttr.add("[" + at + "]");
        });
        item0.add(String.join("", allAttr));
        // 显示代码，跳转代码，自动勾选代码
        item0.add(dispSelect == 1 ? dispCode : "");
        item0.add(jumpSelect == 1 ? jumpCode : "");
        item0.add(autoCode);
        items.set(0, item0);

        // 处理排他设置
        boolean optionsExcludeEffect = (Boolean) questionDict.getOrDefault("optionsExcludeEffect", false); // 是否启用 选项分组
        JSONArray optionExcludeList = questionDict.getJSONArray("option_exclude_list"); // 排他设置
        if (optionsExcludeEffect && optionExcludeList != null && !optionExcludeList.isEmpty()) {
            items.add(Arrays.asList("排他设置", "", "", "", "", ""));
            for (int i = 0; i < optionExcludeList.size(); i++) {
                JSONObject optionGroup = optionExcludeList.getJSONObject(i);
                String opgTitle = optionGroup.getString("title");
                JSONArray opgItems = Optional.ofNullable(optionGroup.getJSONArray("items")).orElse(new JSONArray());
                boolean isExclude = optionGroup.getBoolean("isExclude");
                List<String> ogQcids = new ArrayList<>();
                for (int ind = 0; ind < opgItems.size(); ind++) {
                    JSONObject otmp = opgItems.getJSONObject(ind);
                    ogQcids.add(otmp.getString("oid"));
                }
                items.add(Arrays.asList(opgTitle, String.join(",", ogQcids),
                        isExclude ? "[组内排他]" : ""));
            }
        }
        return items;
    }

    @Override
    public String getType() {
        return "matrix_multiple";
    }
}
